const fs = require("fs");
const config_build = require("../configs/build/index");

const filePath = "./.nuxt/dist/package/client/app_setting.js";
const newContent = `(() => {
  let o = ${JSON.stringify(config_build.appSetting)};
  let ua = navigator.userAgent.toLowerCase();
  let isWX = ua.indexOf("micromessenger") !== -1 && !!window.wx;
  if (!isWX) {
    delete o.UserCenterUrl;
  }
  return o;
})();`;

// 读取文件内容
fs.readFile(filePath, "utf8", (err, data) => {
  if (err) {
    console.error("读取配置出错:", err);
    return;
  }
  // 使用正则表达式匹配等号前的内容
  const match = data.match(/^([^=]+)/);
  if (match) {
    // 获取匹配到的内容（等号前的部分）
    const contentBeforeEqualSign = match[1].trim();
    fs.writeFile(filePath, `${contentBeforeEqualSign}=${newContent}`, "utf8", (err2) => {
      if (err2) {
        console.error("重写配置出错:", err2);
        return;
      }
      console.log("重写配置成功！");
    });
  } else {
    console.log("配置文件中未找到等号。");
  }
});
